<script setup lang="ts">
import APIBill from '@/api/bill'
import SelfPagination from '@/components/SelfPagination/index.vue'
import type { OrderGoodsData } from '@/api/bill/types'
import { useAuth } from '@/stores/index'
import { saveAs } from 'file-saver'
import { ElMessage } from 'element-plus'

const { info } = storeToRefs(useAuth())

const queryParams = ref<PageQuery>({
    page: 1,
    limit: 8
})

const queryData = ref<OrderGoodsData>({
    type: 'goods_consum',
    goods_type: 'all'
})
const list = ref([])

const total = shallowRef<number>(0)

const loading = shallowRef<boolean>(false)

function getSalesLedger() {
    loading.value = true
    APIBill.getSalesLedger(queryParams.value, queryData.value).then(({ data, total: count }) => {
        list.value = data
        total.value = count
    }).finally(() => {
        loading.value = false
    })
} getSalesLedger()

watch(queryData, getSalesLedger, { deep: true })


const time = ref<any>([])

watch(time, (newValue) => {
    if (!newValue) {
        queryData.value.st = undefined
        queryData.value.et = undefined
        return
    }
    queryData.value.st = newValue[0]
    queryData.value.et = newValue[1]
})


function handleClear() {
    time.value = []
    queryData.value = {
        type: 'goods_consum',
        goods_type: 'video'
    }
}

function handleExportExcel() {
    if (list.value.length == 0) return ElMessage.error('暂无内容导出')
    APIBill.exportRecharge(queryData.value).then(({ data }) => {
        saveAs(data, '销售台账.xlsx')
    })
}

</script>

<template>
    <div class="bill">
        <div class="search"></div>
        <div class="title">销售台账</div>
        <el-tabs v-model="queryData.type" class="tabs">
            <el-tab-pane label="自有产品" name="goods_consum">
            </el-tab-pane>
            <el-tab-pane label="搬运产品" name="transport_goods_consum">
            </el-tab-pane>
            <el-tab-pane label="被打赏产品" name="reward">
            </el-tab-pane>
        </el-tabs>
        <div class="condition">
            <div class="left">
                <el-select v-model="queryData.goods_type" placeholder="Select" size="large" style="width: 120px">
                    <el-option label="全部" value="all" />
                    <el-option label="视频" value="video" />
                    <el-option label="图片" value="picture" />
                    <el-option label="音乐" value="music" />
                    <el-option label="字体" value="font" />
                </el-select>
                <el-date-picker v-model="time" type="datetimerange" size="large" range-separator="至"
                    start-placeholder="开始时间" end-placeholder="结束时间" style="margin-left: 20px;" value-format="X" />
            </div>
            <div class="right">
                <div class="gross">
                    总利润：<span>¥{{ info.shop_wallet }}</span>
                </div>
                <button class="derive" @click="handleExportExcel">导出excel</button>
                <div class="clear" @click="handleClear">清除筛选</div>
            </div>
        </div>
        <el-table class="list" :data="list" style="margin: 0 auto; margin-top: 30px; width: 99%;"
            :header-cell-style="{ background: '#f5f7fa', color: '#606266' }" v-loading="loading">
            <el-table-column prop="order_code" label="订单" />
            <el-table-column prop="goods_code" label="编号" />
            <el-table-column label="缩略图" v-slot="{ row }">
                <el-image style="width: 80px; height: 53px" :src="row.image" :preview-src-list="[row.image]" fit="cover"
                    :z-index="999" preview-teleported hide-on-click-modal />
            </el-table-column>
            <el-table-column prop="price" label="收益" />
            <el-table-column prop="create_time" label="购买时间" />
            <el-table-column prop="buyer" label="买家" />
        </el-table>
        <SelfPagination style="margin-top: 50px;margin-right: 30px;" :total="total"
            v-model:current-page="queryParams.page" :page-size="queryParams.limit" @get-list="getSalesLedger" />
    </div>
</template>

<style lang="scss">
.bill {
    .list {
        .el-table__row {
            font-family: 'SOURCEHANSANSCN-EXTRALIGHT';
        }

        .cell {
            font-family: 'SOURCEHANSANSCN-EXTRALIGHT';
        }
    }
}
</style>

<style lang="scss">
.bill {
    .list {
        .el-table__row {
            font-family: 'SOURCEHANSANSCN-EXTRALIGHT';
        }
    }
}
</style>

<style lang="scss" scoped>
.bill {
    margin: 0 auto;
    width: 99%;

    .condition {
        display: flex;
        justify-content: space-between;

        .right {
            display: flex;
            align-items: center;
            font-family: 'SOURCEHANSANSCN-LIGHT';

            .gross {
                height: 22px;
                color: rgba(187, 187, 187, 1);
                font-size: 14px;
                line-height: 22px;
                border-right: 2px solid #BBBBBB;
                padding-right: 15px;

                span {
                    height: 22px;
                    line-height: 22px;
                    color: rgba(248, 50, 0, 1);
                }
            }

            .derive {
                width: 80px;
                height: 28px;
                border-radius: 5px;
                background-color: rgba(24, 131, 232, 1);
                color: #ffffff;
                font-size: 14px;
                border: none;
                cursor: pointer;
                margin: 0 15px;
                font-family: 'SOURCEHANSANSCN-LIGHT';
            }

            .clear {
                height: 28px;
                line-height: 28px;
                width: 56px;
                color: rgba(187, 187, 187, 1);
                font-size: 14px;
                cursor: pointer;
            }
        }
    }

    .content {
        margin-bottom: 30px;

        .list {
            width: 99.99%;
            margin: 0 auto;
        }
    }

    .title {
        color: rgba(249, 20, 20, 1);
        font-size: 24px;
        font-weight: 700;
    }

    .tabs {
        margin-top: 10px;


        &:deep(.el-tabs__item.is-active) {
            color: #F91414;
        }

        &:deep(.el-tabs__active-bar) {
            background-color: #F91414;
            height: 1px;
        }

        &:deep(.el-tabs__item) {
            &:hover {
                color: #F91414;
            }
        }

        &:deep(.el-tabs__nav-wrap) {
            &:after {
                height: 1px !important;
            }
        }
    }
}
</style>
